<?php

namespace app\models\search;

use function var_dump;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\ArcProgress;

/**
 * ArcProgressSearchModel represents the model behind the search form of `app\models\ArcProgress`.
 */
class ArcProgressSearchModel extends ArcProgress
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['arc_train', 'progress', 'arc_classify', 'arc_classify_status'], 'integer'],
            [['arc_language', 'arc_problems'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = ArcProgress::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'arc_train' => $this->arc_train,
            'thing_recordfile' => $this->thing_recordfile,
            'progress' => $this->progress,
            'arc_classify' => $this->arc_classify,
            'arc_classify_status' => $this->arc_classify_status,
            'create_time' => $this->create_time,
        ]);
        if(empty($this->arc_language)){
            $arc_language_json = null;
        }else{
            $arc_language_json = json_encode($this->arc_language);
            $arc_language_json = str_replace("\"","",$arc_language_json);
        }
        $query->andFilterWhere(['like', 'arc_language', $arc_language_json])
            ->andFilterWhere(['like', 'arc_problems', $this->arc_problems]);

        //根据时间筛选记录的列表
        if(isset($_GET['timeZones'])){
            $timeZones = $_GET['timeZones'];
            $startDate = trim(explode('-',$timeZones)[0]);
            $startDate = str_replace('年', '-', $startDate);
            $startDate = str_replace('月', '-', $startDate);
            $startDate = str_replace('日', ' ', $startDate);
            $endDate = trim(explode('-',$timeZones)[1]);
            $endDate = str_replace('年', '-', $endDate);
            $endDate = str_replace('月', '-', $endDate);
            $endDate = str_replace('日', ' ', $endDate);
            $query->andFilterWhere(['between', 'create_time',
                strtotime($startDate),
                strtotime($endDate)
            ]);
        }
        if(isset($_GET['arc_classify_status'])){
            $query->andFilterWhere(['arc_classify_status'=>$_GET['arc_classify_status']]);
        }

        return $dataProvider;
    }
}
